package com.example.rtsp.controller;

import com.example.rtsp.handler.VideoStreamHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author: 杜科璇
 * @Description: 流控制器
 * @Date: 2025/9/5 18:43
 * @Version: 1.0
 */


@RestController
@RequestMapping("/api") // 修改为/api而不是/rtsp
public class StreamController {

    @Autowired
    private VideoStreamHandler videoStreamHandler;

    // 获取流状态
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("activeConnections", videoStreamHandler.getActiveSessionCount());
        status.put("status", "RUNNING");
        return ResponseEntity.ok(status);
    }

    // 启动流
    @PostMapping("/start")
    public ResponseEntity<String> startStreaming() {
        videoStreamHandler.startStreaming();
        return ResponseEntity.ok("视频流已启动");
    }

    // 停止流
    @PostMapping("/stop")
    public ResponseEntity<String> stopStreaming() {
        videoStreamHandler.stopStreaming();
        return ResponseEntity.ok("视频流已停止");
    }

    // 暂停流
    @PostMapping("/pause")
    public ResponseEntity<String> pauseStreaming() {
        videoStreamHandler.pauseStreaming();
        return ResponseEntity.ok("视频流已暂停");
    }

    // 恢复流
    @PostMapping("/resume")
    public ResponseEntity<String> resumeStreaming() {
        videoStreamHandler.resumeStreaming();
        return ResponseEntity.ok("视频流已恢复");
    }
}